<?php


namespace backend\models\c2\entity;


use common\models\c2\entity\Product;
use common\models\c2\entity\ProductCombination;
use common\models\c2\entity\ProductionCommitNoteItem;
use common\models\c2\entity\ProductionScheduleItem;
use common\models\c2\statics\ProductionCommitNoteState;
use common\models\c2\statics\ProductionScheduleState;
use Yii;
use yii\helpers\ArrayHelper;
use yii\validators\RequiredValidator;

class MixtureCommitNote extends \common\models\c2\entity\ProductionCommitNote
{
    // public function rules()
    // {
    //     return ArrayHelper::merge(parent::rules(), [
    //         [['items'], 'validateItems']
    //     ]);
    // }
    //
    // public function validateItems($attribute)
    // {
    //     $items = $this->$attribute;
    //
    //     foreach ($items as $index => $row) {
    //         $requiredValidator = new RequiredValidator();
    //         $error = null;
    //         $requiredValidator->validate($row['product_id'], $error);
    //         $requiredValidator->validate($row['combination_id'], $error);
    //         if (!empty($error)) {
    //             $key = $attribute . '[' . $index . '][product_id]';
    //             // $this->addError($key, Yii::t('app.c2', 'Product/Combination/Package not be null.'));
    //             $this->addError($key, Yii::t('app.c2', '{attribute} can not be empty!', ['attribute' => Yii::t('app.c2', 'Product')]));
    //         }
    //         if (!empty($error)) {
    //             $key = $attribute . '[' . $index . '][combination_id]';
    //             // $this->addError($key, Yii::t('app.c2', 'Product/Combination/Package not be null.'));
    //             $this->addError($key, Yii::t('app.c2', '{attribute} can not be empty!', ['attribute' => Yii::t('app.c2', 'Product Combination')]));
    //         }
    //     }
    // }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if (!empty($this->items)) {
            foreach ($this->items as $item) {
                $product = Product::findOne($item['product_id']);
                if (!is_null($product)) {
                    $attributes = [
                        'schedule_id' => $this->schedule_id,
                        'product_id' => $product->id,
                        'product_name' => $product->name,
                        'product_sku' => $product->sku,
                        'product_label' => $product->label,
                        'product_value' => $product->value,
                        'combination_id' => 0,
                        'combination_name' => '',
                        'measure_id' => isset($item['measure_id']) ? $item['measure_id'] : 0,
                        'commit_sum' => isset($item['commit_sum']) ? $item['commit_sum'] : 0,
                        'memo' => isset($item['memo']) ? $item['memo'] : '',
                        'position' => isset($item['position']) ? $item['position'] : 0,
                    ];
                    if (isset($item['id']) && $item['id'] == '') {
                        $itemModel = new ProductionCommitNoteItem();
                        $itemModel->setAttributes($attributes);
                        $itemModel->link('owner', $this);
                    } elseif (isset($item['id'])) {
                        $itemModel = ProductionCommitNoteItem::findOne($item['id']);
                        if (!is_null($itemModel)) {
                            $itemModel->updateAttributes($attributes);
                        }
                    }
                }
            }
        }
    }

    public function setStateToCommit()
    {
        $ts = Yii::$app->db->beginTransaction();
        $items = $this->getNoteItems()->all();
        if (!empty($items)) {
            foreach ($items as $item) {
                $product = $item->product;
                $product->stock += $item->commit_sum;
                $product->save();

                $scheduleItem = ProductionScheduleItem::find()
                    ->where(['schedule_id' => $item->schedule_id, 'product_id' => $item->product_id])->one();
                $scheduleItem->enter_sum += $item->commit_sum;
                $scheduleItem->save();
            }
        }
        $ts->commit();
        $this->updateAttributes(['state' => ProductionCommitNoteState::COMMIT]);
    }

    public function allMixtureFinish()
    {
        $this->updateAttributes(['state' => ProductionScheduleState::FINISH]);
    }

}